package com.smyh.UIM.device;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.bean.DeviceParam5;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.commcache.commd.UIParamOptionUtil;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceParam05Fm extends Fragment implements View.OnClickListener {
    private Button param5ReadBtn;
    private static TextView mockOverlimitVarElec,mockOverlimitVarPercent,mockReloadUpElec,allWorkElec
        //rf参数设置，中级功能开关
            ,rfParamSet,midFuncSwitch;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_param05,container,false);
        initView(view);
        initEvent();
        return view;
    }
    private void initEvent() {
        mockOverlimitVarElec.setOnClickListener(this);
        mockOverlimitVarPercent.setOnClickListener(this);
        mockReloadUpElec.setOnClickListener(this);
        allWorkElec.setOnClickListener(this);
        rfParamSet.setOnClickListener(this);
        midFuncSwitch.setOnClickListener(this);
        param5ReadBtn.setOnClickListener(btnClickListenrer);
    }
    private View.OnClickListener btnClickListenrer = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.param5ReadBtn:{
                    if(MinaClientThread.session == null){
                        Toast.makeText(getContext(),"请连接wifi！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    IoBuffer buf = CMDUtil.askOutParam(HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr), (byte) 5);
                    MinaClientThread.session.write(buf);
                    Toast.makeText(getContext(),"读取数据！",Toast.LENGTH_SHORT).show();
                    break;
                }
                default:{
                    break;
                }
            }
        }
    };
    private void initView(View view) {
        mockOverlimitVarElec = (TextView) view.findViewById(R.id.mockOverlimitVarElec);
        mockOverlimitVarPercent = (TextView) view.findViewById(R.id.mockOverlimitVarPercent);
        mockReloadUpElec = (TextView) view.findViewById(R.id.mockReloadUpElec);
        allWorkElec = (TextView) view.findViewById(R.id.allWorkElec);
        rfParamSet = (TextView) view.findViewById(R.id.rfParamSetText);
        midFuncSwitch = (TextView) view.findViewById(R.id.midFuncSwitchText);

        param5ReadBtn = (Button) view.findViewById(R.id.param5ReadBtn);
    }

    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            DeviceParam5 obj = DeviceParamInfoCache.getDeviceParamMap5().get(MainCahce.chooseDeviceAddr);
            if (obj != null){

                mockOverlimitVarElec.setText(obj.getMockOverlimitVarElec()+"");
                mockOverlimitVarPercent.setText(obj.getMockOverlimitVarPercent()+"");
                mockReloadUpElec.setText(obj.getMockReloadUpElec()+"");
                allWorkElec.setText(obj.getAllWorkElec()+"");
                rfParamSet.setText(obj.getRFParamSet()+"");
                midFuncSwitch.setText(obj.getMidFnSwitch()+"");
            }else {
                mockOverlimitVarElec.setText( " ");
                mockOverlimitVarPercent.setText( " ");
                mockReloadUpElec.setText( " ");
                allWorkElec.setText( " ");
                rfParamSet.setText( "  ");
                midFuncSwitch.setText( "  ");

            }
        }
    };
    public static void freshDeviceParam5Model() {
        handler1.sendMessage(new Message());
    }
    int index = 1;
    String str = "";
    @Override
    public void onClick(View v) {
        if(MinaClientThread.session==null){
            Toast.makeText(getContext(), "请连接wifi", Toast.LENGTH_SHORT).show();
            return;
        }
        if(MainCahce.chooseDeviceAddr.equals("")){
            Toast.makeText(getContext(), "请选择设备", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()){
            case R.id.mockOverlimitVarElec:{
                index = 1;
                str = mockOverlimitVarElec.getText().toString();
                break;
            }
            case R.id.mockOverlimitVarPercent:{
                index = 2;
                str = mockOverlimitVarPercent.getText().toString();
                break;
            }
            case R.id.mockReloadUpElec:{
                index = 3;
                str = mockReloadUpElec.getText().toString();
                break;
            }
            case R.id.allWorkElec:{
                index = 4;
                str = allWorkElec.getText().toString();
                break;
            }
            case R.id.rfParamSetText:{
                index = 5;
                str = rfParamSet.getText().toString();
                break;
            }
            case R.id.midFuncSwitchText:{
                index = 6;
                str = midFuncSwitch.getText().toString();
                break;
            }
        }
        final EditText edit = new EditText(getContext());
        edit.setText(str);
        edit.setSelection(str.length());
        new AlertDialog.Builder(getContext()).setTitle("请输入").setView(edit)
                .setPositiveButton("修改", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    byte[] addrs = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                    IoBuffer buffer = UIParamOptionUtil.writeDeviceParm(addrs,5,index,Integer.valueOf(edit.getText().toString()));
                    MinaClientThread.session.write(buffer);
                }catch (Exception e){
                    Toast.makeText(getContext(), "格式不正确", Toast.LENGTH_SHORT).show();
                }
            }
        }) .setNegativeButton("取消", null).show();
    }

}
